package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"post-pub/cache"
	"post-pub/model"
	"strconv"
	"time"
)

func DepotAdd(c *gin.Context) {
	depot_code := c.PostForm("depot_code")
	depot_name := c.PostForm("depot_name")
	depot_statue := c.PostForm("depot_statue")
	if depot_name == "" || depot_code == "" || depot_statue == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := model.Depot{
		DepotCode:   depot_code,
		DepotName:   depot_name,
		DepotStatue: depot_statue,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "仓库信息添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "仓库信息添加成功",
	})
	return
}

func DepotOrderAdd(c *gin.Context) {
	depot_id, _ := strconv.Atoi(c.PostForm("depot_id"))
	order_code := c.PostForm("order_code")
	order_people := c.PostForm("order_people")

	data := model.DepotOrder{
		DepotId:     depot_id,
		OrderCode:   order_code,
		OrderPeople: order_people,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "入库订单添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "入库订单添加成功",
	})
	return
}
func DepotOrderList(c *gin.Context) {
	/*
		SELECT
		  depots.depot_code as DepotCode,
		  depots.depot_name as DepotName,
		  depots.depot_statue as DepotStatue,
		  depot_orders.depot_id as DepotId,
		  depot_orders.order_code as OrderCode,
		  depot_orders.order_people as OrderPeople

		FROM
		  depot_orders
		  LEFT JOIN depots ON depots.id = depot_orders.depot_id
	*/
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	var total int64
	model.DB.Model(&model.DepotOrder{}).Count(&total)
	var data []model.DepotOrderList
	redis_key := "depotOrder_list_" + strconv.Itoa(int(page))
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("depot_orders").
			Select(
				"depots.depot_code as DepotCode",
				"depots.depot_name as DepotName",
				"depots.depot_statue as DepotStatue",
				"depot_orders.depot_id as DepotId",
				"depot_orders.order_code as OrderCode",
				"depot_orders.order_people as OrderPeople",
			).
			Joins("LEFT JOIN depots ON depots.id = depot_orders.depot_id").
			Offset(offset).Limit(size).Debug().Find(&data)
		jsonData, _ := json.Marshal(data)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*1)
	} else {
		json.Unmarshal([]byte(cacheData), &data)
	}
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"total": total,
		"data":  data,
	})
	return
}
func DepotOrderDetail(c *gin.Context) {
	/*
		SELECT
		  depots.depot_code as DepotCode,
		  depots.depot_name as DepotName,
		  depots.depot_statue as DepotStatue,
		  depot_orders.depot_id as DepotId,
		  depot_orders.order_code as OrderCode,
		  depot_orders.order_people as OrderPeople

		FROM
		  depot_orders
		  LEFT JOIN depots ON depots.id = depot_orders.depot_id
		WHERE depot_orders.depot_id=1
	*/
	depot_id := c.Query("depot_id")
	var data []model.DepotOrderList
	redis_key := "depotOrder_detail_" + depot_id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("depot_orders").
			Select(
				"depots.depot_code as DepotCode",
				"depots.depot_name as DepotName",
				"depots.depot_statue as DepotStatue",
				"depot_orders.depot_id as DepotId",
				"depot_orders.order_code as OrderCode",
				"depot_orders.order_people as OrderPeople",
			).
			Joins("LEFT JOIN depots ON depots.id = depot_orders.depot_id").
			Where("depot_orders.depot_id=?", depot_id).
			Debug().Find(&data)
		jsonData, _ := json.Marshal(data)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*1)
	} else {
		json.Unmarshal([]byte(cacheData), &data)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "订单详细查询",
		"data": data,
	})
	return
}
